package org.china.lanzhou.grid;

import org.china.lanzhou.exception.SystemSequenceAlreadyExistException;

import java.util.ArrayList;
import java.util.List;

public class StepSequenceGeneration implements SequenceGeneration{

    int systemLength = 20000;
    int now = systemLength;
    int step = 1;

    public StepSequenceGeneration() {
    }

    public StepSequenceGeneration(int step) {
        this.step = step;
    }

    public StepSequenceGeneration(int systemLength, int now) {
        this.systemLength = systemLength;
        this.now = now;
    }

    public StepSequenceGeneration(int systemLength, int now, int step) {
        this.systemLength = systemLength;
        this.now = now;
        this.step = step;
    }

    List<Integer> freeSequence = new ArrayList<>(); // 普通级主动释放
    List<Integer> activeSequence = new ArrayList<>(); // 普通级主动占用
    int[] systemSequence = new int[systemLength];  // 系统级占用
    /**
     * 创一个新鲜的
     *
     * @return
     */
    @Override
    public int getSequence() {
        int index = 0;
        int size = freeSequence.size();
        if(size>0){
            index = freeSequence.get(size);
            freeSequence.remove(size);
            return index;
        }
            while (!activeSequence.contains(now+=step)){
                return now;
            }
                                                                                                                                            return index;
    }

    /**
     * 添加占位
     *
     * @param index
     */
    @Override
    public void setSequence(Integer index) {
        if(index<systemSequence.length){
            if(systemSequence[index]!=1){
                systemSequence[index]=1;
            }else{
                throw new SystemSequenceAlreadyExistException();
            }
            return;
        }
        if(!freeSequence.contains(index)){
            freeSequence.remove(index);
        }
        if(!activeSequence.contains(index)){
            activeSequence.add(index);
        }
    }


    /**
     * 删除
     *
     * @param index
     */
    @Override
    public void remove(Integer index) {
        if(index<systemSequence.length){
            systemSequence[index]=0;
        }
        activeSequence.remove(index);
        if(!freeSequence.contains(index))
        freeSequence.add(index);
    }

    /**
     * 获取类型
     *
     * @param index
     */
    @Override
    public String getType(int index) {
        if(systemSequence.length>index){
            return "System";
        }
        if(activeSequence.contains(index)){
            return "Active";
        }
        if(freeSequence.contains(index)){
            return "Free";
        }
        return "Average";
    }

    public static void main(String[] args) {
        StepSequenceGeneration stepSequenceGeneration = new StepSequenceGeneration();
//        stepSequenceGeneration.setSequence(20000);
        System.out.println(stepSequenceGeneration.getSequence());
    }


}
